package com.chen.stopwatch.common;

import android.view.MotionEvent;

public class DirectionMotionDetector {
	private static MotionEvent latestEvent = null;
	private static MotionEvent startPoint = null;

	public final static int MaxVerticalOffset = 20;

	public final static int MotionUnknown = 0;
	public final static int MotionVerticalScroll = 1;
	public final static int MotionHorizontalScroll = 2;

	public void addMotionEvent(MotionEvent paramMotionEvent) {
		latestEvent = MotionEvent.obtain(paramMotionEvent);
	}

	public int checkFling() {
		int v = (int) ((latestEvent.getY() - startPoint.getY()) / (latestEvent
				.getEventTime() - startPoint.getEventTime()));
		return v;
	}

	public int motionDetect() {
		if (null == startPoint || null == latestEvent)
			return MotionUnknown;

		float xOffset = Math.abs(startPoint.getX() - latestEvent.getX());
		float yOffset = Math.abs(startPoint.getY() - latestEvent.getY());

		if (0 == xOffset && 0 == yOffset) {
			return MotionUnknown;
		}

		if (Math.abs(xOffset - yOffset) < MaxVerticalOffset) {
			return MotionVerticalScroll;
		}

		if (xOffset > yOffset) {
			return MotionHorizontalScroll;
		} else {
			return MotionVerticalScroll;
		}
	}

	public void start(MotionEvent paramMotionEvent) {
		startPoint = MotionEvent.obtain(paramMotionEvent);
	}
}
